package com.hebang.stcommunityapp;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;

import com.facebook.drawee.backends.pipeline.Fresco;

import java.util.ArrayList;



public class MyApplication extends Application implements Application.ActivityLifecycleCallbacks{
    public static Context APP_CONTEXT;

    private static Application samApplication;

    private static MyApplication mApplication;

    ArrayList<Activity> list = new ArrayList<Activity>();


    //本机mac地址
    public static String mac;

    private int activityCount;//activity的count数
    public static boolean isForeground;//是否在前台



    public static interface  CallLinsenner{
        void onVideoCall(Context context, String from, String type);
        void onVoiceCall(Context context, String from, String type);
    }


    @Override
    public void onCreate() {
        super.onCreate();

        registerActivityLifecycleCallbacks(this);
        APP_CONTEXT = getApplicationContext();
        mApplication = this;

        //当程序发生Uncaught异常的时候,由该类来接管程序,一定要在这里初始化
//        CrashHandler.getInstance().init(this);
//        if(ConstantTools.catchCrash == 1){
//            CrashHandler catchHandler = CrashHandler.getInstance();
//            catchHandler.init(getApplicationContext());
//        }

        //设置该CrashHandler为程序的默认处理器
//        UnCeHandler catchExcep = new UnCeHandler(this);
//        Thread.setDefaultUncaughtExceptionHandler(catchExcep);

        Fresco.initialize(this);
//        JPushInterface.setDebugMode(true);
//        JPushInterface.init(this);
//        //注册微信
//        api = WXAPIFactory.createWXAPI(this, null);
//        api.registerApp(ConstantTools.APP_ID);

//        if (isMainProcess()) {
//            OpenInstall.init(this);
//        }


        // android 7.0系统解决拍照的问题
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            builder.detectFileUriExposure();
        }

//        ShareSDK.initSDK(this);
//        initPhoneData();


    }

    public static Context getmContext() {
        return APP_CONTEXT;
    }



    public boolean isMainProcess() {
        int pid = android.os.Process.myPid();
        ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess : activityManager.getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                return getApplicationInfo().packageName.equals(appProcess.processName);
            }
        }
        return false;
    }


    public static Application getSamApplication() {
        return samApplication;
    }


    public void initPhoneData() {

        //wifi管理
        WifiManager wifi = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifi.getConnectionInfo();
        //获取wifi设备的mac码
        mac = info.getMacAddress();
    }


    public static MyApplication getInstance() {
        return mApplication;
    }

    public static Context getAppContext() {
        return APP_CONTEXT;
    }

    public static MyApplication getApplication(){
        return mApplication;
    }


//    private static DiskLruStringCache mStringCache = null;
//
//    public static DiskLruStringCache getDiskLruStringCache(){
//        if(mStringCache==null){
//            mStringCache = new DiskLruStringCache(getApplication(),"NewsCache");
//        }
//        return mStringCache;
//    }

    /**
     * Activity关闭时，删除Activity列表中的Activity对象*/
    public void removeActivity(Activity a){
        list.remove(a);
    }

    /**
     * 向Activity列表中添加Activity对象*/
    public void addActivity(Activity a){
        list.add(a);
    }

    /**
     * 关闭Activity列表中的所有Activity*/
    public void finishActivity(){
        for (Activity activity : list) {
            if (null != activity) {
                activity.finish();
            }
        }
        //杀死该应用进程
        android.os.Process.killProcess(android.os.Process.myPid());
    }


    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

    }

    @Override
    public void onActivityStarted(Activity activity) {

    }

    @Override
    public void onActivityResumed(Activity activity) {
    }

    @Override
    public void onActivityPaused(Activity activity) {

    }

    @Override
    public void onActivityStopped(Activity activity) {



    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {

    }



}
